package com.zhang.communication.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.communication.dto.AnnounceSearchDTO;
import com.zhang.communication.entity.Bulletin;
import com.zhang.communication.mapper.BulletinMapper;
import com.zhang.communication.service.IBulletinService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 系统通告表 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2024-06-06
 */
@Service
public class BulletinServiceImpl extends ServiceImpl<BulletinMapper, Bulletin> implements IBulletinService {

    @Override
    public void createBulletin(Bulletin bulletin) {
        bulletin.setPublish(StpUtil.getLoginIdAsString());
        bulletin.setCreateTime(LocalDateTime.now());
        save(bulletin);
    }

    @Override
    public Page<Bulletin> getBulletinList(AnnounceSearchDTO announceSearchDTO) {
        LambdaQueryWrapper<Bulletin> wrapper = new LambdaQueryWrapper<>();
        if (!StrUtil.isBlank(announceSearchDTO.getKeyWord())) {
            wrapper.or().eq(Bulletin::getPublish, announceSearchDTO.getKeyWord())
                    .or().eq(Bulletin::getId, announceSearchDTO.getKeyWord())
                    .or().like(Bulletin::getTitle, announceSearchDTO.getKeyWord());
        }
        wrapper.eq(!StrUtil.isBlank(announceSearchDTO.getTargetType()), Bulletin::getTargetType, announceSearchDTO.getTargetType());
        wrapper.eq(announceSearchDTO.getStatus() != -1, Bulletin::getStatus, announceSearchDTO.getStatus());
        return page(new Page<>(announceSearchDTO.getPageNum(), announceSearchDTO.getPageSize()), wrapper);
    }

    @Override
    public List<Bulletin> getUsingBulletin() {
        LambdaQueryWrapper<Bulletin> wrapper = new LambdaQueryWrapper<>();
//        TODO 如果公告指定了通知范围，则该方法需要更改
        wrapper.eq(Bulletin::getStatus, 0);
        return list(wrapper);
    }
}
